import os
from PyQt5.QtWidgets import QWidget, QVBoxLayout, QLabel, QHBoxLayout
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QFont
from qfluentwidgets import SmoothScrollArea, PrimaryPushButton, InfoBar, InfoBarPosition, IconWidget, FluentIcon


class ClearInterface(SmoothScrollArea):
    def __init__(self, parent=None):
        super().__init__(parent=parent)
        self.view = QWidget(self)
        self.vBoxLayout = QVBoxLayout(self.view)
        self.setWidget(self.view)
        self.setWidgetResizable(True)

        # 将背景样式直接应用到主视图，修改背景图片路径
        self.view.setStyleSheet('''
            QWidget#mainView {
                border-image: url(pic/background2.png) 0 0 0 0 stretch stretch;
            }
        ''')
        self.view.setObjectName("mainView")

        # 确保滚动区域和其他组件保持透明
        self.setStyleSheet('''
            QScrollArea {
                border: none;
                background: transparent;
            }
            QLabel {
                background: transparent;
                color: white;
            }
            QPushButton {
                border: none;
                background-color: transparent;
            }
        ''')

        self.initUI()

    def initUI(self):
        self.vBoxLayout.setAlignment(Qt.AlignCenter)
        self.vBoxLayout.setSpacing(40)  # 减小整体间距

        # 添加顶部空白
        self.vBoxLayout.addSpacing(120)  # 减少顶部空白

        # 添加标题
        title = QLabel("清除数据")
        title_font = QFont("楷体", 36, QFont.Bold)
        title.setFont(title_font)
        title.setAlignment(Qt.AlignCenter)
        self.vBoxLayout.addWidget(title)

        # 间距
        self.vBoxLayout.addSpacing(40)  # 减小间距

        # 修改这里：使用正确的 FluentIcon 枚举值
        icon = IconWidget(FluentIcon.CANCEL, self)  # 使用 CANCEL 代替 CLOSE_CIRCLE
        icon.setFixedSize(64, 64)
        self.vBoxLayout.addWidget(icon, alignment=Qt.AlignCenter)

        # 间距
        self.vBoxLayout.addSpacing(20)  # 减小间距

        # 添加警告说明
        warning_text = QLabel(
            "此操作将删除以下数据：\n\n"
            "• 环境矩阵文件\n"
            "• 路径点对数据\n"
            "• 象限处理结果\n"
            "• 规划路径数据\n"
            "• 统计分析数据\n\n"
            "此操作不可撤销，请谨慎操作！"
        )
        warning_text.setFont(QFont("Microsoft YaHei", 14))
        warning_text.setAlignment(Qt.AlignCenter)
        self.vBoxLayout.addWidget(warning_text)

        # 间距
        self.vBoxLayout.addSpacing(30)  # 减小间距

        # 添加清除按钮，使用水平布局确保居中
        button_layout = QHBoxLayout()
        button_layout.setAlignment(Qt.AlignCenter)

        self.clear_button = PrimaryPushButton('清除所有数据')
        self.clear_button.setFixedWidth(240)
        self.clear_button.setFixedHeight(50)
        self.clear_button.setFont(QFont("Microsoft YaHei", 13))
        self.clear_button.clicked.connect(self.clear_data)
        button_layout.addWidget(self.clear_button)

        self.vBoxLayout.addLayout(button_layout)

        # 添加底部弹性空间
        self.vBoxLayout.addStretch()

    def clear_data(self):
        try:
            current_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
            data_dir = os.path.join(current_dir, 'data')

            files_to_clear = [
                'total_cost.txt',
                'planning_results.json',
                'generated_matrix.txt',
                'point_pairs.txt',
                'combined_output.txt',
                'quadrant_visualization.png'
            ]

            # 清除每个象限的文件
            for i in range(1, 5):
                files_to_clear.extend([
                    f'quadrant_{i}.txt',
                    f'quadrant_{i}.animation.json'
                ])

            for filename in files_to_clear:
                file_path = os.path.join(data_dir, filename)
                if os.path.exists(file_path):
                    with open(file_path, 'w') as f:
                        f.write('')

            InfoBar.success(
                title='成功',
                content='所有数据文件已清除',
                orient=Qt.Horizontal,
                isClosable=True,
                position=InfoBarPosition.TOP,
                duration=2000,
                parent=self
            )

        except Exception as e:
            InfoBar.error(
                title='错误',
                content=f'清除数据时出错: {str(e)}',
                orient=Qt.Horizontal,
                isClosable=True,
                position=InfoBarPosition.TOP,
                duration=4000,
                parent=self
            )